const fetch = require('node-fetch')

const request = async (url, option) => {
    const headers = {
        'Content-Type': 'application/json',
    }
    const host = 'http://127.0.0.1:18000'
    const options = {
        headers: headers,
        mode: 'cors',
        ...option,
    };

    url = host + url;
    return fetch(url, options)
                .then(resp=>{
                    return resp.json();
                })
                .then(
                    json => {
                        if (json.code !== 0) {
                            console.log("异常出现了：" + json.msg)
                        }
                        return json;
                    }
                )
                .catch( e => {
                    console.log("服务异常")
                    return {code: 10000}
                })
}

module.exports = {
  request
}